package com.jucai.pppeiyouclient.view.fragment;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.AccompanyRecordAdapter;
import com.jucai.pppeiyouclient.base.BaseFragment;
import com.jucai.pppeiyouclient.bean.AccompanyRecordBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Kuky on 2017-05-06.
 */

public class AccompanyRecordFragment extends BaseFragment implements PullToRefreshLayout.OnRefreshListener {
    private static final String TAG = "AccompanyRecordFragment : ";
    private PullToRefreshLayout refreshLayout;
    private PullToRefreshForRecyclerView accompanyRecordRv;
    private List<AccompanyRecordBean> recordList;
    private AccompanyRecordAdapter recordAdapter;
    private Handler mHandler = new Handler();
    private int page = 1;

    @Override
    protected void initView() {
        recordAdapter = new AccompanyRecordAdapter(getActivity(), recordList);
        refreshLayout = (PullToRefreshLayout) itemView.findViewById(R.id.refresh_view);
        accompanyRecordRv = (PullToRefreshForRecyclerView) itemView.findViewById(R.id.content_view);
        accompanyRecordRv.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        accompanyRecordRv.setItemAnimator(new DefaultItemAnimator());
        accompanyRecordRv.setAdapter(recordAdapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Conductor_Person.getlist");
        param.put("id", getActivity().getIntent().getStringExtra("user_id"));
        param.put("page", "1");
        LogUtils.e(TAG + "param = " + param.toString());
        CommonRequestNetData.getJsonObjectData(getActivity(), param,true, new CommonRequestNetData.JsonObjectCallBack() {
            @Override
            public void parseJsonObject(JSONObject jsonObject) {
                LogUtils.e(TAG + "jsObj Accompany = " + jsonObject.toString());
                try {
                    JSONArray jsonArray = jsonObject.getJSONArray("py_history");
                    recordList = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<AccompanyRecordBean>>() {
                    }.getType());
                    LogUtils.e(TAG + "recordList = " + recordList);
                    recordAdapter.setData(recordList);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected int getFragmentViewId() {
        return R.layout.common_fragment_with_ptr;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadNetData();
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
        ++page;
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Conductor_Person.getlist");
                param.put("id", getActivity().getIntent().getStringExtra("user_id"));
                param.put("page", page + "");
                LogUtils.e(TAG + "param = " + param.toString());
                CommonRequestNetData.getJsonObjectData(getActivity(), param,true, new CommonRequestNetData.JsonObjectCallBack() {
                    @Override
                    public void parseJsonObject(JSONObject jsonObject) {
                        LogUtils.e(TAG + "jsObj = " + jsonObject.toString());
                        try {
                            JSONArray jsonArray = jsonObject.getJSONArray("py_history");
                            List<AccompanyRecordBean> list = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<AccompanyRecordBean>>() {
                            }.getType());
//                            recordList.addAll(list);
                            LogUtils.e(TAG + "recordList = " + recordList);
                            recordAdapter.setData(recordList);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);

    }
}
